package com.qf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.blog.commons.exception.BusinessException;
import com.qf.blog.commons.security.UserTokenThreadLocal;
import com.qf.blog.entity.DiscussPost;
import com.qf.blog.entity.PostPage;
import com.qf.blog.entity.User;
import com.qf.blog.enums.ExceptionEnum;
import com.qf.blog.mapper.CommentMapper;
import com.qf.blog.mapper.PostMapper;
import com.qf.blog.mapper.UserMapper;
import com.qf.blog.service.IPostService;
import com.qf.blog.service.LikeService;
import com.qf.blog.vo.LikeVo;
import com.qf.blog.vo.PostVo;
import com.qf.blog.vo.SinglePostVo;
import com.qf.blog.vo.UserToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, PostVo> implements IPostService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private LikeService likeService;

    public PostVo postVoMapper(Object _item) {
        PostVo item = (PostVo) _item;
        LikeVo likeVo = new LikeVo();
        likeVo.setEntityId(item.getId());
        likeVo.setEntityType(1);
        likeVo.setUserId(item.getUserId());
        Long likeCount = likeService.getLikeCount(likeVo);
        boolean likeStatus = likeService.getLikeStatus(likeVo);
        item.setLikeCount(likeCount);
        item.setLikeStatus(likeStatus);
        return item;
    }

    @Override
    public PostPage queryPage(PostPage page) {
        PostPage postPage = baseMapper.queryPage(page);
        List<PostVo> _records = postPage.getRecords();
        List<PostVo> records =
                _records.stream().map(this::postVoMapper).collect(Collectors.toList());
        postPage.setRecords(records);
        return postPage;
    }

    @Override
    public SinglePostVo queryById(Integer id) {
        DiscussPost discussPost = baseMapper.selectById(id);
        Integer userId = discussPost.getUserId();
        User user = userMapper.selectById(userId);
        SinglePostVo singlePostVo = new SinglePostVo();
        singlePostVo.setUser(user);
        singlePostVo.setPost(discussPost);
        return singlePostVo;
    }

    @Override
    public Boolean publish(PostVo post) {
        if (!StringUtils.isNoneBlank(post.getTitle(), post.getContent())) {
            throw new BusinessException(ExceptionEnum.POST_FIELD_EMPTY);
        }
        UserToken userToken = UserTokenThreadLocal.get();
        post.setUserId(userToken.getUserId());
        return baseMapper.insert(post) > 0;
    }

    @Override
    public Boolean updateScore(Integer postId, Double score) {
        UpdateWrapper<PostVo> updateWrapper = new UpdateWrapper<PostVo>()
                .eq("id", postId)
                .set("score", score);
        return baseMapper.update(null, updateWrapper) > 0;
    }
}
